package com.zhangyu.shadow.core.util.toast;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;


public class ToastUtils {

	/** 上下文. */
	private static Context mContext = null;

	/** 显示Toast. */
	public static final int SHOW_TOAST = 0;

	private static String oldMsg;
	protected static Toast toast = null;
	private static long oneTime = 0;
	private static long twoTime = 0;

	/**
	 * 主要Handler类，在线程中可用 what：0.提示文本信息
	 */
	private static Handler baseHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOW_TOAST:
				showToast(mContext, msg.getData().getString("TEXT"));
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 描述：在线程中提示文本信息.
	 * 
	 * @param resId
	 *            要提示的字符串资源ID，消息what值为0,
	 */
	public static void showToastInThread(Context context, int resId) {
		mContext = context;
		Message msg = baseHandler.obtainMessage(SHOW_TOAST);
		Bundle bundle = new Bundle();
		bundle.putString("TEXT", context.getResources().getString(resId));
		msg.setData(bundle);
		baseHandler.sendMessage(msg);
	}

	/**
	 * 描述：在线程中提示文本信息.
	 *
	 */
	public static void showToastInThread(Context context, String text) {
		mContext = context;
		Message msg = baseHandler.obtainMessage(SHOW_TOAST);
		Bundle bundle = new Bundle();
		bundle.putString("TEXT", text);
		msg.setData(bundle);
		baseHandler.sendMessage(msg);
	}

	/**
	 * 描述：Toast提示文本.
	 * 
	 * @param context
	 *            上下文
	 * @param s
	 *            文本
	 */
	public static void showToast(Context context, String s) {
		if (toast == null) {
			toast = Toast.makeText(context, s, Toast.LENGTH_SHORT);
			toast.show();
			oneTime = System.currentTimeMillis();
		} else {
			twoTime = System.currentTimeMillis();
			if (s.equals(oldMsg)) {
				if (twoTime - oneTime > Toast.LENGTH_SHORT) {
					toast.show();
				}
			} else {
				oldMsg = s;
				toast.setText(s);
				toast.show();
			}
		}
		oneTime = twoTime;
	}

	/**
	 * 描述：Toast提示文本.
	 * 
	 * @param context
	 *            上下文
	 * @param resId
	 *            文本的资源ID
	 */
	public static void showToast(Context context, int resId) {
		showToast(context, context.getString(resId));
	}

}
